<template>
  <div class="register">
    <div class="box">
      <h1>在线教育管理系统</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
  <el-form-item label="用户名" prop="name">
    <el-input v-model="ruleForm.name"></el-input>
  </el-form-item>
  <el-form-item label="邮箱" prop="email">
    <el-input v-model="ruleForm.email"></el-input>
  </el-form-item>
  <el-form-item label="密码"  prop="password">
    <el-input v-model="ruleForm.password" type="password"></el-input>
  </el-form-item>
  <el-form-item label="确认密码" prop="password2">
    <el-input v-model="ruleForm.password2" type="password"></el-input>
  </el-form-item>
  <el-form-item label="身份">
    <el-select v-model="ruleForm.identity" style="
  text-align: left" placeholder="请选择身份">
      <el-option label="管理员" value="管理员"></el-option>
    </el-select>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" style="width: 100%;" @click="submit"  >注册</el-button>
  </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name:'register',
  data() {
    return {
      ruleForm:{},
      rules:{
         name: [
            { required: true, message: '请输入用户名', trigger: ['blur','change'] },
          ],
          email:[
            { required: true, message: '请输入邮箱', trigger: ['blur','change'] },
          ],
          password:[
            { required: true, message: '请输入密码', trigger: ['blur','change'] },
          ],
          password2:[
            { required: true, message: '请确认密码', trigger: ['blur','change'] },
          ],
          identity:[
            { required: true, message: '请选择身份', trigger: ['blur','change'] },
          ],
      }
    }
  },
  methods: {
    // https://www.thenewstep.cn/v1/vue3system/api/users/register
    submit(){
      this.$refs.ruleForm.validate(async (valid) => {
          if (!valid) {
           this.$message('请补全信息')
          } 
          var res = await this.$http({
            url:'/v1/vue3system/api/users/register',
            method: 'post',
            data:this.ruleForm
          })
          console.log(res);
          if(res.status==200){
            this.$message('注册成功,请登录')
          }
          this.$router.push({name:'login'})
          this.ruleForm={
              name:"",
              email:"",
              password:"",
              password2:"",
              identity:"",
              }
        });
    }
  },
}
</script>
<style lang="scss" scoped>
.register{
  width: 100vw;
  height: 100vh;
  background: #2d3a4b;
  overflow: hidden;
  .box{
    width: 500px;
    height: 500px;
    margin: auto;
    margin-top: 50px;
    h1{
      color: #fff;
      // text-align: center;
      font-size: 30px;
      margin-bottom: 20px;
      margin-left: 80px;
    }
  }
}
</style>